/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.PermissionForm;
import com.ntb.models.PermissionsDAO;
import com.ntb.models.UserGroupsDAO;
import com.ntb.models.impl.PermissionsDAOImpl;
import com.ntb.models.impl.UserGroupsDAOImpl;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class PermissionAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        ActionForward forward = mapping.getInputForward();
        PermissionForm permissionForm = (PermissionForm) form;
        if ("save".equalsIgnoreCase(permissionForm.getAction())) {
            PermissionsDAO permissionDao = new PermissionsDAOImpl();

            UserGroupsDAO groupDao = new UserGroupsDAOImpl();

            request.setAttribute("permissionsTable", permissionDao.getPermissionsTable());
            request.setAttribute("group", groupDao.getRow(permissionForm.getGroupID()));

            updatePermissions(permissionForm, request);
            
        } else if ("Save & Exit".equalsIgnoreCase(permissionForm.getAction())) {
            updatePermissions(permissionForm, request);
            
            forward = mapping.findForward("permissionListing");
        } else if (permissionForm.getGroupID() > 0) {
            PermissionsDAO permissionDao = new PermissionsDAOImpl();

            UserGroupsDAO groupDao = new UserGroupsDAOImpl();

            String modules[] = new String[20];

            int i = 0;
            for (Iterator<String> it = permissionDao.getPermissionsList(permissionForm.getGroupID()).iterator(); it.hasNext();) {
                modules[i] = it.next();
                i++;
            }

            permissionForm.setGroupID(permissionForm.getGroupID());
            permissionForm.setModules(modules);

            request.setAttribute("permissionsTable", permissionDao.getPermissionsTable());
            request.setAttribute("group", groupDao.getRow(permissionForm.getGroupID()));
        } else {
            forward = mapping.findForward("permissionListing");
        }
        return forward;
    }

    private void updatePermissions(PermissionForm form, HttpServletRequest request) {
        if (form.getGroupID() > 0) {
            if (form.getGroupID() == 1) {
                request.setAttribute("errorMessage", "Sorry, the permissions for this group could not be saved.");
            } else {
                PermissionsDAO permissionDao = new PermissionsDAOImpl();
                permissionDao.updatePermissions(form.getModules(), form.getGroupID());
                request.setAttribute("successMessage", "The permissions for this group have been saved.");
            }
        }
    }
}
